/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra.adapter;

import java.util.LinkedList;
import java.util.Vector;

import net.cachapa.libra.data.Value;
import net.cachapa.libra.util.UnitManager;
import net.cachapa.libra.util.Util;
import net.cachapa.libra.Preferences;
import net.cachapa.libra.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class ValuesAdapter implements ListAdapter, SectionIndexer {
	private Context context;
	private Vector<Value> values;
	private LinkedList<DataSetObserver> observers;
	private float goalWeight;

	public ValuesAdapter(Context context, Vector<Value> values) {
		this.context = context;
		this.values = values;
		this.observers = new LinkedList<DataSetObserver>();
		
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		this.goalWeight = prefs.getFloat(Preferences.GOAL_WEIGHT, 0);
	}
	
	public void refreshValues(Vector<Value> values) {
		this.values = values;
		notifyObservers();
	}
	
	public void deleteValue(int position) {
		values.remove(position);
		notifyObservers();
	}
	
	public void deleteAllValues() {
		values.removeAllElements();
		notifyObservers();
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int position) {
		return true;
	}

	public int getCount() {
		return values.size();
	}

	public Value getItem(int position) {
		return values.get(position);
	}

	public long getItemId(int position) {
		return (long)values.get(position).getId();
	}

	public int getItemViewType(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		LayoutInflater inflater;
		Value value = values.get(position);
		view = new View(context);
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.databaseitem, null);		
		
		TextView dateTextView = (TextView)view.findViewById(R.id.date);
		dateTextView.setText(value.getDate().toString());
		
		UnitManager um = UnitManager.getInstance(context);
		TextView valueTextView = (TextView)view.findViewById(R.id.weight);
		valueTextView.setText(um.toWeightUnit(value.getWeight()));
		
		TextView trendTextView = (TextView)view.findViewById(R.id.trend);
		trendTextView.setText(um.toWeightUnit(value.getTrend()));
		
		float variance = Util.Round(value.getWeight() - value.getTrend(), 1);
		String varianceString;
		if (variance > 0) {
			varianceString = "+" + variance;
		}
		else {
			varianceString = String.valueOf(variance);
		}
		TextView varianceTextView = (TextView)view.findViewById(R.id.variance);
		varianceTextView.setText(varianceString);
		
		if (goalWeight > 0) {
			// This is used to check if the variance is approaching the goal weight, so we can color the text accordingly
			if (goalWeight - value.getTrend() > 0) {
				variance *= -1;
			}
			if (variance > 0) {
				varianceTextView.setTextColor(Color.RED);
			}
			else if (variance < 0) {
				varianceTextView.setTextColor(Color.GREEN);
			}
		}
		
		return view;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isEmpty() {
		return values.isEmpty();
	}
	
	public void registerDataSetObserver(DataSetObserver observer) {
		observers.add(observer);
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		observers.remove(observer);
	}
	
	private void notifyObservers() {
		for (DataSetObserver observer : observers) {
			observer.onChanged();
		}
	}

	public int getPositionForSection(int section) {
		return section;
	}

	public int getSectionForPosition(int position) {
		return position;
	}

	public Object[] getSections() {
		return new String[] {"Fake"};
	}
}
